package com.shen.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class ApiInvocationUtil {

    // 使用HttpURLConnection发送GET请求
    public static String sendGetRequest(String url) throws IOException {
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();

        // 设置请求方法
        con.setRequestMethod("GET");

        // 添加请求头
        con.setRequestProperty("User-Agent", "Mozilla/5.0");

        // 获取响应码
        int responseCode = con.getResponseCode();
        System.out.println("发送GET请求到URL: " + url);
        System.out.println("响应码: " + responseCode);

        // 读取响应内容
        try (BufferedReader in = new BufferedReader(
                new InputStreamReader(con.getInputStream()))) {
            String inputLine;
            StringBuilder response = new StringBuilder();
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            return response.toString();
        }
    }

    // 使用HttpURLConnection发送POST请求
    public static String sendPostRequest(String url, String requestBody) throws IOException {
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();

        // 设置请求方法
        con.setRequestMethod("POST");

        // 添加请求头
        con.setRequestProperty("Content-Type", "application/json");
        con.setRequestProperty("User-Agent", "Mozilla/5.0");

        // 启用输出流，用于发送请求体
        con.setDoOutput(true);

        // 写入请求体
        try (OutputStream os = con.getOutputStream()) {
            byte[] input = requestBody.getBytes(StandardCharsets.UTF_8);
            os.write(input, 0, input.length);
        }

        // 获取响应码
        int responseCode = con.getResponseCode();
        System.out.println("发送POST请求到URL: " + url);
        System.out.println("请求体: " + requestBody);
        System.out.println("响应码: " + responseCode);

        // 读取响应内容
        try (BufferedReader in = new BufferedReader(
                new InputStreamReader(con.getInputStream()))) {
            String inputLine;
            StringBuilder response = new StringBuilder();
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            return response.toString();
        }
    }

    // 使用Apache HttpClient发送GET请求
    public static String sendGetRequestWithHttpClient(String url) throws ClientProtocolException, IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);

        // 添加请求头
        httpGet.addHeader("User-Agent", "Mozilla/5.0");

        try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
            System.out.println("发送GET请求到URL: " + url);
            System.out.println("响应码: " + response.getStatusLine().getStatusCode());

            // 获取响应实体
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                return EntityUtils.toString(entity);
            }
            return null;
        }
    }

    // 使用Apache HttpClient发送POST请求
    public static String sendPostRequestWithHttpClient(String url, String requestBody,String authorization) throws ClientProtocolException, IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);

        // 添加请求头
        httpPost.addHeader("Content-Type", "application/json");
        httpPost.addHeader("User-Agent", "Mozilla/5.0");
        httpPost.addHeader("Authorization", "Bearer "+authorization);

        // 设置请求体
        StringEntity entity = new StringEntity(requestBody, "UTF-8");
        httpPost.setEntity(entity);

        try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
            System.out.println("发送POST请求到URL: " + url);
            System.out.println("请求体: " + requestBody);
            System.out.println("响应码: " + response.getStatusLine().getStatusCode());

            // 获取响应实体
            HttpEntity responseEntity = response.getEntity();
            if (responseEntity != null) {
                return EntityUtils.toString(responseEntity);
            }
            return null;
        }
    }


}    